<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Operators in Visual Basic</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Visual Basic, learn Visual Basic, operators, programming, language">
<meta name="description" content="This part of the Visual Basic tutorial 
covers operators in Visual Basic.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Operators</h1>

<p>
In this part of the Visual Basic tutorial, we will talk about operators. 
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div>

<p>
An <b>operator</b> is a special symbol which indicates a certain process is carried out. 
Operators in programming languages are taken from mathematics. Programmers work with 
data. The operators are used to process data. 
</p>

<p>
We have several types of operators:
</p>

<ul>
<li>Arithmetic operators</li>
<li>Boolean operators</li>
<li>Relational operators</li>
<li>Bitwise operators</li>
</ul>

<p>
An operator may have one or two operands. An <b>operand</b> is one of the inputs (arguments) of an operator. 
Those operators that work with only one operand are called <b>unary operators</b>. 
Those who work with two operands are called <b>binary operators</b>. 
</p>


<pre class="code">
Option Strict On

Module Example

    Sub Main()

        Console.WriteLine(2)
        Console.WriteLine(-2)
        Console.WriteLine(2+2)
        Console.WriteLine(2-2)

    End Sub

End Module
</pre>

<p>
+ and - signs can be addition and subtraction operators as well as unary sign operators. 
It depends on the situation.
</p>

<pre class="code">
Option Strict On

Module Example

    Dim a As Byte

    Sub Main()

        a = 1
        Console.WriteLine(-a)    ' Prints -1
        Console.WriteLine(-(-a)) ' Prints 1

    End Sub

End Module
</pre>

<p>
The plus sign can be used to indicate that we have a positive number. 
But it is mostly not used. The minus sign changes the sign of a value.  
</p>


<pre class="code">
Option Strict On


Module Example

    Dim a As Byte
    Dim b As Byte

    Sub Main()

        a = 3 * 3
        b = 2 + 2   
        
        Console.WriteLine(a) ' Prints 9
        Console.WriteLine(b) ' Print 4

    End Sub

End Module
</pre>

<p>
Multiplication and addition operators are examples of binary operators. They are used with two operands.  
</p>


<h2>The assignment operator</h2>

<p>
The assignment operator <b>=</b> assigns a value to a variable. A <b>variable</b> is a placeholder for a value. 
In mathematics, the <b>=</b> operator has a different meaning. In an equation,
the <b>=</b> operator is an equality operator. The left side of the equation is equal to the right one. 
</p>

<pre class="code">
x = 1
Console.WriteLine(x) ' Prints 1
</pre>

<p>
Here we assign a number to the x variable. 
</p>

<pre class="code">
x = x + 1
Console.WriteLine(x)
</pre>

<p>
The previous expression does not make sense in mathematics. But it is legal in programming. 
The expression adds 1 to the x variable. The right side is equal to 2 and 2 is assigned to x. 
</p>

<pre class="code">
3 = x
</pre>

<p>
This code example results in syntax error. We cannot assign a value to a literal. 
</p>

<h2>Arithmetic operators</h2>

<p>
The following is a table of arithmetic operators in Visual Basic. 
</p>

<table>
<tr>
<th>Symbol</th><th>Name</th>
</tr>
<tr><td>+</td><td>Addition</td></tr>
<tr class="gray"><td>-</td><td>Subtraction</td></tr>
<tr><td>*</td><td>Multiplication</td></tr>
<tr class="gray"><td>/</td><td>Division</td></tr>
<tr><td>\</td><td>Integer Division</td></tr>
<tr class="gray"><td>Mod</td><td>Modulo</td></tr>
<tr><td>^</td><td>Exponentiation</td></tr>
</table>


<p>
The following example shows arithmetic operations.
</p>

<pre class="code">
Option Strict On


Module Example

    Dim a As Byte
    Dim b As Byte
    Dim c As Byte

    Dim add As Byte
    Dim sb As Byte
    Dim mult As Byte
    Dim div As Byte

    Sub Main()

        a = 10
        b = 11
        c = 12

        add = a + b + c
        sb = c - a
        mult = a * b
        div = CType(c / 3, Byte)
        
        Console.WriteLine(add)
        Console.WriteLine(sb)
        Console.WriteLine(mult)
        Console.WriteLine(div)

    End Sub

End Module
</pre>

<p>
In the preceding example, we use addition, subtraction, multiplication and
division operations. This is all familiar from the mathematics.
</p>

<pre>
$ ./arithmetic.exe 
33
2
110
4
</pre>

<p>
Output of the example. 
</p>

<p>
Next we will show the distinction between normal and integer division.
</p>

<pre class="code">
Option Strict On


Module Example

    Dim a As Single = 5
    Dim b As Single = 2
    Dim c As Single

    Sub Main()

        c = 5 / 2 
        Console.WriteLine(c)
        c = 5 \ 2
        Console.WriteLine(c)
        
    End Sub

End Module
</pre>

<p>
In the preceding example, we divide two numbers using normal
and integer division operator. Visual Basic has two distinct operators
for division. 
</p>

<pre class="explanation">
Dim a As Single = 5
</pre>

<p>
We use floating point data types. 
</p>

<pre class="explanation">
c = 5 / 2 
Console.WriteLine(c)
</pre>

<p>
This is the 'normal' division operation. It returns 2.5, as expected. 
</p>

<pre class="explanation">
c = 5 \ 2
Console.WriteLine(c)
</pre>

<p>
This is integer division. The result of this operation is always and
integer. The c variable has value 2. 
</p>

<pre>
$ ./division.exe 
2.5
2
</pre>

<p>
Result of the division.exe program. 
</p>

<hr class="btm">

<p>
The last two operators that we will mention are modulo 
operator and exponentiation operator. 
</p>

<pre class="code">
Console.WriteLine(9 Mod 4) ' Prints 1
</pre>

<p>
The <code>Mod</code> operator is called the modulo operator. 
It finds the remainder of division of one number by another. 
<code>9 Mod 4</code>, 9 modulo 4 is 1, because 4 goes into 9 
twice with a remainder of 1. Modulo operator can be handy for example
when we want to check for prime numbers. 
</p>

<p>
Finally, we will mention <b>exponentiation</b> operator. 
</p>

<pre class="code">
Console.WriteLine(9 ^ 2) ' Prints 81
</pre>

<p>
9 ^ 2 = 9 * 9 = 81
</p>


<h2>Concatenating strings</h2>

<p>
In Visual Basic we have two operators for string concatenation. 
The plus + operator and the &amp; ampersand operator. 
</p>


<pre class="code">
Option Strict On


Module Example

    Sub Main()

        Console.WriteLine("Return " &amp; "of " &amp; "the king")
        Console.WriteLine("Return " + "of " + "the king")

    End Sub

End Module
</pre>

<p>
We join three strings together using both operators. 
</p>

<pre>
$ ./concatstrings.exe 
Return of the king
Return of the king
</pre>

<p>
And this is, what we get. Same result for both cases.  
</p>



<h2>Boolean operators</h2>

<p>
In Visual Basic, we have the following logical operators. 
Boolean operators are also called logical. 
</p>

<table>
<tr>
<th>Symbol</th><th>Name</th>
</tr>
<tr><td>And</td><td>logical conjunction</td></tr>
<tr class="gray"><td>AndAlso</td><td>short circuit And</td></tr>
<tr><td>Or</td><td>logical inclusion</td></tr>
<tr class="gray"><td>OrElse</td><td>short circuit Or</td></tr>
<tr><td>Xor</td><td>logical inclusion</td></tr>
<tr class="gray"><td>Not</td><td>negation</td></tr>
</table>

<p>
Boolean operators are used to work with truth values. 
</p>

<pre class="code">
Option Strict On


Module Example

    Dim x As Byte = 3
    Dim y As Byte = 8

    Sub Main()

        Console.WriteLine(x = y) 
        Console.WriteLine(y > x)

        If (y > x)
            Console.WriteLine("y is greater than x")
        End If

    End Sub

End Module
</pre>

<p>
Many expressions result in a boolean value. Boolean values are used
in conditional statements. 
</p>

<pre class="explanation">
Console.WriteLine(x = y) 
Console.WriteLine(y > x)
</pre>

<p>
Relational operators always result in a Boolean value. These two lines
print False and True. 
</p>

<pre class="explanation">
If (y > x)
    Console.WriteLine("y is greater than x")
End If
</pre>

<p>
The body of the <code>If</code> statement
is executed only if the condition inside the parentheses
is met. The x > y returns True, so the message "y is greater than x"
is printed to the terminal. 
</p>


<pre class="code">
Option Strict On


Module Example

    Dim a As Boolean
    Dim b As Boolean
    Dim c As Boolean
    Dim d As Boolean

    Sub Main()

        a = (True And True)
        b = (True And False)
        c = (False And True)
        d = (False And False)

        Console.WriteLine(a)
        Console.WriteLine(b)
        Console.WriteLine(c)
        Console.WriteLine(d)


    End Sub

End Module
</pre>

<p>
Example shows the logical <code>And</code> operator. 
It evaluates to True only if both operands are True.
</p>

<pre>
$ ./andop.exe 
True
False
False
False
</pre>

<hr class="btm">

<p>
The logical <code>Xor</code> operator evaluates to True, 
if exactly one of the operands is True. 
</p>

<pre class="code">
Option Strict On


Module Example

    Dim a As Boolean
    Dim b As Boolean
    Dim c As Boolean
    Dim d As Boolean

    Sub Main

        a = (True Xor True)
        b = (True Xor False)
        c = (False Xor True)
        d = (False Xor False)

        Console.WriteLine(a)
        Console.WriteLine(b)
        Console.WriteLine(c)
        Console.WriteLine(d)

    End Sub

End Module
</pre>

<p>
The logical <code>Xor</code> evaluates to False, if
both operands are True or both False. 
</p>

<pre>
$ ./xorop.exe 
False
True
True
False
</pre>

<hr class="btm">

<p>
The logical <code>Or</code> operator evaluates to True, 
if either of the operands is True.
</p>

<pre class="code">
Option Strict On


Module Example

    Sub Main()

        Dim a As Boolean = True Or True
        Dim b As Boolean = True Or False
        Dim c As Boolean = False Or True
        Dim d As Boolean = False Or False

        Console.WriteLine(a)
        Console.WriteLine(b)
        Console.WriteLine(c)
        Console.WriteLine(d)

    End Sub

End Module
</pre>

<p>
If one of the sides of the operator is True, the outcome of the operation is True. 
</p>

<pre>
$ ./orop.exe 
True
True
True
False
</pre>

<hr class="btm">

<p>
The <b>negation</b> operator <code>Not</code> makes True False and False True. 
</p>

<pre class="code">
Option Strict On


Module Example

    Sub Main()

        Console.WriteLine(Not True)
        Console.WriteLine(Not False)
        Console.WriteLine(Not (4 &lt; 3))

    End Sub

End Module
</pre>

<p>
The example shows the negation operator in action.
</p>

<pre>
$ ./negation.exe 
False
True
True
</pre>

<hr class="btm">

<p>
<code>AndAlso</code>, <code>OrElse</code> operators are short circuit evaluated. 
<b>Short circuit evaluation</b> means that the second argument is only evaluated 
if the first argument does not suffice to determine the value of the expression: when the 
first argument of And evaluates to false, the overall value must be false; and when the 
first argument of Or evaluates to true, the overall value must be true. (wikipedia)
Short circuit evaluation is used mainly to improve performance. 
</p>

<p>
An example may clarify this a bit more.  
</p>

<pre class="code">
Option Strict On


Module Example

    Sub Main()

        Console.WriteLine("Short circuit")
        If (one AndAlso two)
            Console.WriteLine("Pass")
        End If

        Console.WriteLine("#############")
        If (one And two)
            Console.WriteLine("Pass")
        End If

    End Sub

Function one As Boolean
    Console.WriteLine("Inside one")
    Return False
End Function

Function two As Boolean
    Console.WriteLine("Inside two")
    Return True
End Function

End Module
</pre>

<p>
We have two functions in the example. Functions, unlike subroutines, return
values. This is the main difference between them. 
</p>

<pre class="explanation">
If (one AndAlso two)
    Console.WriteLine("Pass")
End If
</pre>

<p>
The one function returns False. The short circuit <code>AndAlso</code>
does not evaluate the second function. It is not necessary.
Once an operand is False, the result of the logical conclusion is always
False. Only "Inside one" is printed to the console. 
</p>

<pre class="explanation">
Console.WriteLine("#############")
If (one And two)
    Console.WriteLine("Pass")
End If
</pre>

<p>
In the second case, we use the <code>And</code>.
In this case, both functions are called. Even though it
is not necessary for the result of the expression. 
</p>


<pre>
$ ./shorcircuit.exe 
Short circuit
Inside one
#############
Inside one
Inside two
</pre>

<p>
Result of the shorcircuit.exe program. 
</p>


<h2>Relational Operators</h2>

<p>
Relational operators are used to compare values. These operators always result in a boolean value. 
</p>

<table>
<tr>
<th>Symbol</th><th>Meaning</th>
</tr>
<tr><td>&lt;</td><td>less than</td></tr>
<tr class="gray"><td>&lt;=</td><td>less than or equal to</td></tr>
<tr><td>&gt;</td><td>greater than</td></tr>
<tr class="gray"><td>&gt;=</td><td>greater than or equal to</td></tr>
<tr><td>==</td><td>equal to</td></tr>
<tr class="gray"><td>&lt;&gt;</td><td>not equal to</td></tr>
<tr><td>Is</td><td>compares references</td></tr>
</table>

<p>
Relational operators are also called comparison operators.
</p>

<pre class="code">
Console.WriteLine(3 &lt; 4) ' Prints True
Console.WriteLine(3 = 4) ' Prints False
Console.WriteLine(4 &gt;= 3) ' Prints True
</pre>

<p>
As we already mentioned, the relational operators return boolean values. 
Note that in Visual Basic, the comparison operator is (=). Not (==) like in
C and C influenced languages.  
</p>

<p>
Notice that the relational operators are not limited to numbers. 
We can use them for other objects as well. Although they might not 
always be meaningful. 
</p>

<pre class="code">
Option Strict On


Module Example


    Sub Main()

        Console.WriteLine("six" = "six") ' Prints True
        ' Console.WriteLine("a" &gt; 6) this would throw
                                     ' an exception 
        Console.WriteLine("a" &lt; "b") ' Prints True

    End Sub

End Module
</pre>

<p>
We can compare string objects too. Comparison operators in a string
context compare the sorting order of the characters. 

</p>

<pre class="explanation">
Console.WriteLine("a" &lt; "b") ' Prints True
</pre>

<p>
What exactly happens here? Computers do not know characters or strings. For them, everything is just a number. 
Characters are special numbers stored in specific tables. Like ASCII. 
</p>

<pre class="code">
Option Strict On

Module Example

    Sub Main()

        Console.WriteLine("a" &lt; "b")
        
        Console.WriteLine("a is: {0}",  Asc("a"))
        Console.WriteLine("b is: {0}",  Asc("b"))
        
    End Sub

End Module
</pre>

<p>
Internally, the a and b characters are numbers. So when we compare two characters,
we compare their stored numbers. The built-in <code>Asc</code> function 
returns the ASCII value of a single character. 
</p>

<pre>
$ ./compare.exe 
True
a is: 97
b is: 98
</pre>

<p>
In fact, we compare two numbers. 97 with 98. 
</p>

<pre class="code">
Console.WriteLine("ab" > "aa") ' Prints True
</pre>

<p>
Say we have a string with more characters. If the first characters are equal, 
we compare the next ones. In our case, the b character at the second position 
has a greater value than the a character. That is why "ab" string is greater 
than "aa" string. Comparing strings in such a way does not make much sense, of course.
But it is technically possible.
</p>

<p>
Finally, we will mention the <code>Is</code> operator. 
The operator checks if two object references refer to the same object. 
It does not perform value comparisons.
</p>

<pre class="code">
Option Strict On


Module Example

    Sub Main()

        Dim o1 As Object = New Object
        Dim o2 As Object = New Object
        Dim o3 As Object 
        
        o3 = o2

        Console.WriteLine(o1 Is o2)
        Console.WriteLine(o3 Is o2)

    End Sub

End Module
</pre>

<p>
We create three objects and compare them with the <code>Is</code> operator. 
</p>

<pre class="explanation">
Dim o1 As Object = New Object
Dim o2 As Object = New Object
</pre>

<p>
We declare and initialize two Object instances. The Object class is a base class
for all classes in the .NET framework. We will describe it later in more detail. 
</p>

<pre class="explanation">
Dim o3 As Object 
</pre>

<p>
The third variable is only declared. 
</p>

<pre class="explanation">
o3 = o2
</pre>

<p>
The o3 now refers to the o2 object. They are two references to the
same object. 
</p>

<pre class="explanation">
Console.WriteLine(o1 Is o2)
Console.WriteLine(o3 Is o2)
</pre>

<p>
In the first case, we get False. o1 and o2 are two different object. 
In the second case, we get True. o3 and o2 refer to the same object. 
</p>


<h2>Bitwise operators</h2>

<p>
Decimal numbers are natural to humans. Binary numbers are native to computers. 
Binary, octal, decimal or hexadecimal symbols are only notations of the same number. 
Bitwise operators work with bits of a binary number. Bitwise operators are seldom 
used in higher level languages like Visual Basic. 
</p>

<table>
<tr>
<th>Symbol</th><th>Meaning</th>
</tr>
<tr><td>Not</td><td>bitwise negation</td></tr>
<tr class="gray"><td>Xor</td><td>bitwise exclusive or</td></tr>
<tr><td>And</td><td>bitwise and</td></tr>
<tr class="gray"><td>Or</td><td>bitwise or</td></tr>
</table>

<p>
The <b>bitwise negation operator</b> changes each 1 to 0 and 0 to 1. 
</p>

<pre class="code">
Console.WriteLine(Not 7)  ' Prints -8
Console.WriteLine(Not -8) ' Prints 7
</pre>

<p>
The operator reverts all bits of a number 7. One of the bits also determines, whether the number is negative or not. 
If we negate all the bits one more time, we get number 7 again. 
</p>

<p>
The <b>bitwise and operator</b> performs bit-by-bit comparison between two numbers. The result for a bit
position is 1 only if both corresponding bits in the operands are 1. 
</p>

<pre>
      00110
  And 00011
   =  00010
</pre>

<p>
The first number is a binary notation of 6. The second is 3. The result is 2. 
</p>

<pre class="code">
Console.WriteLine(6 And 3) ' Prints 2
Console.WriteLine(3 And 6) ' Prints 2
</pre>

<p>
The <b>bitwise or operator</b> performs bit-by-bit comparison between two numbers. 
The result for a bit position is 1 if either of the corresponding bits in the operands is 1. 
</p>

<pre>
     00110
  Or 00011
   = 00111
</pre>

<p>
The result is <code>00110</code> or decimal 7. 
</p>

<pre class="code">
Console.WriteLine(6 Or 3) ' Prints 7
Console.WriteLine(3 Or 6) ' Prints 7
</pre>

<p>
The <b>bitwise exclusive or operator</b> performs bit-by-bit comparison between two numbers. 
The result for a bit position is 1 if one or the other (but not both) of the corresponding bits in the operands is 1. 
</p>

<pre>
      00110
  Xor 00011
   =  00101
</pre>

<p>
The result is <code>00101</code> or decimal 5. 
</p>

<pre class="code">
Console.WriteLine(6 Xor 3) ' Prints 5
</pre>


<h2>Compound assignment operators</h2>

<p>
The compound assignment operators consist of two operators. They are shorthand operators. 
</p>

<pre class="code">
Option Strict On

Module Example

    Dim a As Integer

    Sub Main

        a = 1
        a = a + 1
        Console.WriteLine(a) ' Prints 2

        a += 1
        Console.WriteLine(a) ' Prints 3

    End Sub

End Module
</pre>

<p>
The += compound operator is one of these shorthand operators. 
They are less readable than the full expressions but 
experienced programmers often use them. 
</p>

<p>
Other compound operators are: 
</p>

<pre>
-=   *=   \=   /=   &amp;=  ^= 
</pre>



<h2>Operator precedence</h2>

<p>
The <b>operator precedence</b> tells us which operators are evaluated first. 
The precedence level is necessary to avoid
ambiguity in expressions. 
</p>

<p>
What is the outcome of the following expression? 28 or 40?
</p>

<pre>
 3 + 5 * 5
</pre>

<p>
Like in mathematics, the multiplication operator has a higher 
precedence than addition operator. So the outcome is 28.
</p>

<pre>
(3 + 5) * 5
</pre>

<p>
To change the order of evaluation, we can use parentheses.
Expressions inside parentheses are always evaluated first. 
</p>


<p>
The following list shows common Visual Basic operators ordered by
precedence (highest precedence first):
</p> 
 
<table> 
  <tr> 
    <th>Operator(s)</th> 
    <th>Description</th> 
  </tr> 
  <tr class="gray"> 
    <td><code>^</code></td> 
    <td>exponentiation</td> 
  </tr> 
  <tr> 
    <td><code>+ -</code></td> 
    <td>unary identity and negation</td> 
  </tr> 
  <tr class="gray">
    <td><code>* /</code></td> 
    <td>multiplication, float division</td> 
  </tr> 
  <tr>
    <td><code>\</code></td> 
    <td>integer division</td> 
  </tr> 
  <tr class="gray"> 
    <td><code>Mod</code></td> 
    <td>modulus</td> 
  </tr> 
  <tr>
    <td><code>+ -</code></td> 
    <td>addition, subtraction, string concatenation</td> 
  </tr> 
  <tr class="gray"> 
    <td><code>&amp;</code></td> 
    <td>string concatenation</td> 
  </tr> 
  <tr>
    <td><code>&lt;&lt; &gt;&gt;</code></td> 
    <td>arithmetic bit shift</td> 
  </tr> 
  <tr class="gray"> 
    <td><code>= &lt;&gt; &lt; &gt; &gt;= &lt;= Is IsNot Like TypeOf Is</code></td> 
    <td>All comparison operators</td> 
  </tr> 
  <tr>
    <td><code>Not</code></td> 
    <td>negation</td> 
  </tr> 
  <tr class="gray">
    <td><code>And AndAlso</code></td> 
    <td>conjunction</td> 
  </tr> 
  <tr>
    <td><code>Or OrElse</code></td> 
    <td>Inclusive disjunction</td> 
  </tr> 
  <tr class="gray">
    <td><code>Xor</code></td> 
    <td>Exclusive disjunction</td> 
  </tr> 
</table> 
 
<p>Operators on the same line in the list have the same precedence.</p> 

<pre class="code">
Option Strict On

Module Example

    Sub Main()

        Console.WriteLine(3 + 5 * 5)
        Console.WriteLine((3 + 5) * 5)

        Console.WriteLine(Not True Or True)
        Console.WriteLine(Not (True Or True))

    End Sub

End Module

</pre>

<p>
In this code example, we show some common expressions. 
The outcome of each expression is dependent on the precedence level.
</p>

<pre class="explanation">
Console.WriteLine(3 + 5 * 5)
</pre>

<p>
This line prints 28. The multiplication operator has a higher precedence
than addition. First the product of 5*5 is calculated. Then 3 is added. 
</p>


<pre class="explanation">
Console.WriteLine(Not True Or True)
</pre>

<p>
In this case, the negation operator has a higher precedence. First, the first True 
value is negated to False, than the Or operator 
combines False and True, which gives True in the end. 
</p>

<pre>
$ ./precedence.exe 
28
40
True
False
</pre>


<h2>Associativity</h2>

<p>
Sometimes the precedence is not satisfactory to determine the outcome 
of an expression. There is another rule called
<b>associativity</b>. The associativity of operators determines 
the order of evaluation of operators with the <b>same</b>
precedence level. 
</p>

<pre>
9 / 3 * 3
</pre>

<p>
What is the outcome of this expression? 9 or 1? The multiplication, deletion and the modulo
operator are left to right associated. So the expression is evaluated 
this way: <code>(9 / 3) * 3</code> and the result is 9.
</p>

<p>
Arithmetic, boolean, relational and bitwise operators are all left to right associated. 
</p>


<p>
On the other hand, the assignment operator is right associated. 
</p>

<pre class="code">
a = b = c = d = 0
Console.WriteLine("{0} {1} {2} {3}", a, b, c, d) ' Prints 0 0 0 0 
</pre>

<p>
If the association was left to right, the previous expression would not be possible. 
</p>

<p>
The compound assignment operators are right to left associated. 
</p>

<pre class="code">
j = 0
j *= 3 + 1
Console.WriteLine(j)  
</pre>

<p>
You might expect the result to be 1. But the actual result is 0.
Because of the associativity. The expression on the right is 
evaluated first and than the compound assignment operator is applied.
</p>


<h2>AddressOf operator</h2>

<p>
The <code>AddressOf</code> operator creates a function 
delegate that points to another function.
Delegates are type safe function pointers, they
are used to call methods of other objects. 
</p>

<pre class="code">
Option Strict On


Module Example

    Delegate Sub Display

    Dim msg As New Display(AddressOf Message1)

    Sub Main()

        msg.Invoke()
        msg = New Display(AddressOf Message2)
        msg.Invoke()

    End Sub

    Sub Message1()
        Console.WriteLine("This is message 1")
    End Sub

    Sub Message2()
        Console.WriteLine("This is message 2")
    End Sub

End Module
</pre>

<p>
In the code example, we use the <code>AddressOf</code> operator
to point to two different subroutines. 
</p>


<pre class="explanation">
Delegate Sub Display
</pre>

<p>
We need to declare a delegate. 
</p>

<pre class="explanation">
Dim msg As New Display(AddressOf Message1)
</pre>

<p>
The delegate takes the address of a subroutine using the
<code>AddressOf</code> operator. Now we have a
type-safe pointer to the Message1() subroutine.
</p>

<pre class="explanation">
msg.Invoke()
</pre>

<p>
The <code>Invoke()</code> method calls the method,
to which the delegate points. 
</p>

<pre class="explanation">
msg = New Display(AddressOf Message2)
msg.Invoke()
</pre>

<p>
Now we give the delegate an address of another subroutine. 
</p>

<pre>
$ ./addressof.exe 
This is message 1
This is message 2
</pre>

<p>
Both messages are printed to the console. 
</p>


<p>
In this part of the Visual Basic tutorial, we covered the operators. 
</p>


<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div>
<br>

<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified August 25, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

